package com.coocaa.x.app.liteplayer;

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;

import com.baidu.cyberplayer.core.BVideoView;
import com.baidu.cyberplayer.core.BVideoView.OnCompletionListener;
import com.baidu.cyberplayer.core.BVideoView.OnErrorListener;
import com.baidu.cyberplayer.core.BVideoView.OnInfoListener;
import com.baidu.cyberplayer.core.BVideoView.OnPlayingBufferCacheListener;
import com.baidu.cyberplayer.core.BVideoView.OnPreparedListener;
import com.baidu.cyberplayer.core.BVideoView.OnSeekCompleteListener;
import com.coocaa.x.app.liteplayer.ui.PlayerView;
import com.coocaa.x.app.liteplayer.ui.SkinManager;
import com.coocaa.x.app.liteplayer.ui.SkyConstants;
import com.coocaa.x.app.liteplayer.ui.PlayerView.PlayerExecutor;
import com.coocaa.x.framework.app.CoocaaService.AIDLInterface;
import com.coocaa.x.framework.utils.AsyncTask;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayer;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayerListener;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayerMonitor;
import com.coocaa.x.service.liteplayer.aidl.PlayerKernelInfo;
import com.coocaa.x.service.liteplayer.aidl.ReadyInfo;
import com.coocaa.x.service.liteplayer.aidl.VideoSourceInfo;
import com.skyworth.framework.skysdk.android.SkySystemUtil;

public class PlayerActivity extends Activity implements PlayerExecutor,
        OnPlayingBufferCacheListener, OnCompletionListener, OnErrorListener, OnInfoListener,
        OnPreparedListener, OnSeekCompleteListener {
    private static final String tag = "pa";
    private PlayerView contentView;
    private static final int UpdateProgressPeriod = 1000; // 更新播放进度的时间间隔
    private String videoName = null;
    private Uri nextUri = null;
    private Timer mTimer;
    private TimerTask mTimerTask;

    private BVideoView bvv = null;

    private PlayerKernelInfo info = new PlayerKernelInfo();

    private ILitePlayer.Stub myPlayer = new ILitePlayer.Stub() {
        @Override
        public void getReady(final ReadyInfo info) throws RemoteException {
            // TODO Auto-generated method stub
            Log.i(tag, "getReady -0000-- videoName:" + info.name);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Log.i(tag, "getReady --- videoName:" + info.name);
                    contentView.showBVV(false);
                    if (info.name != null && info.name.trim().length() > 0) {
                        String loadingStr = String.format("%s\n%s", info.name.trim(),
                                PlayerActivity.this.getString(R.string.player_loading));
                        contentView.resetInfo();
                        contentView.startLoading(loadingStr);
                    } else {
                        contentView.startLoading();
                    }

                }
            });
        }

        @Override
        public boolean start(VideoSourceInfo info, String videoName1) throws RemoteException {
            // TODO Auto-generated method stub
            Log.i(tag, "Service - start, uri = " + info.url + ", videoName = " + videoName1
                    + " mPlayerStatus:" + mPlayerStatus.toString());
            if (bvv != null) {
                if (mPlayerStatus != PLAYER_STATUS.PLAYER_IDLE) {
                    Log.i(tag, "Start  need stop player!!!");
                    contentView.setNeedExitActivity(false);
                    bvv.stopPlayback();
                }

                while (mPlayerStatus != PLAYER_STATUS.PLAYER_IDLE) {
                    try {
                        Log.i(tag, "Start  wait for player  ");
                        Thread.currentThread().sleep(10);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                try {
                    Log.i(tag, "Start  wait for player again ");
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                bvv.setVideoPath(info.url);
                videoName = videoName1;
                bvv.start();
                contentView.setNeedExitActivity(true);

                Log.i(tag, "Start  start play");
                mPlayerStatus = PLAYER_STATUS.PLAYER_PREPARING;
                scheduleProgressUpdate();
            }
            return true;
        }

        @Override
        public boolean pause() throws RemoteException {
            // TODO Auto-generated method stub
            Log.i(tag, "Service - pause");
            if (bvv == null) {
                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
                return false;
            }
            unScheduleProgressUpdate();
            bvv.pause();
            contentView.onPlayPaused();
            return true;
        }

        @Override
        public boolean resume() throws RemoteException {
            // TODO Auto-generated method stub
            Log.i(tag, "Service - resume");
            if (bvv == null) {
                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
                return false;
            }
            scheduleProgressUpdate();
            bvv.resume();
            contentView.onPlayResumed();
            return true;
        }

        @Override
        public boolean stop() throws RemoteException {
            // TODO Auto-generated method stub
            Log.i(tag, "Service - stop");
            if (bvv == null) {
                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
                return false;
            }
            videoName = null;
            unScheduleProgressUpdate();
            bvv.stopPlayback();
            contentView.onPlayStopped();
            return true;
        }

        @Override
        public boolean seekTo(int milliSeconds) throws RemoteException {
            // TODO Auto-generated method stub
            Log.i(tag, "Service - seekto  milliSeconds:" + milliSeconds);
            if (bvv == null) {
                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
                return false;
            }
            contentView.showSeekPanel(milliSeconds / 1000);
            bvv.seekTo(milliSeconds / 1000);
            return true;
        }

        @Override
        public boolean isPlaying() throws RemoteException {
            // TODO Auto-generated method stub
            Log.i(tag, "Service - isPlaying");
            if (bvv != null) {
                return bvv.isPlaying();
            }
            Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
            return false;
        }

        @Override
        public int getCurrentTimeInMillis() throws RemoteException {
            // TODO Auto-generated method stub
            Log.i(tag, "Service - getCurrentTimeInMillis");
            if (bvv == null) {
                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
                return -1;
            }
            return bvv.getCurrentPosition();
        }

        @Override
        public int getDuration() throws RemoteException {
            // TODO Auto-generated method stub
            Log.i(tag, "Service - getDuration");
            if (bvv == null) {
                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
                return -1;
            }
            return bvv.getDuration();
        }

        @Override
        public void errorOccurred(String errorType, String videoName1) throws RemoteException {
            // TODO Auto-generated method stub
            videoName = videoName1;
            contentView.errorOccurred(errorType, videoName1);
        }

    };

    private ILitePlayerMonitor.Stub myMonitor = new ILitePlayerMonitor.Stub() {

        @Override
        public void addPlayerListener(ILitePlayerListener arg0) throws RemoteException {
            synchronized (clientListeners) {
                if (arg0 != null) {
                    IBinder binder = arg0.asBinder();
                    AIDLInterface<ILitePlayerListener> i = new AIDLInterface<ILitePlayerListener>(
                            binder, arg0);
                    clientListeners.add(i);
                }
            }
        }

        @Override
        public void removePlayerListener(ILitePlayerListener arg0) throws RemoteException {
            synchronized (clientListeners) {
                if (arg0 != null) {
                    IBinder binder = arg0.asBinder();
                    for (AIDLInterface<ILitePlayerListener> i : clientListeners) {
                        if (i.binder == binder) {
                            clientListeners.remove(i);
                            break;
                        }
                    }
                }
            }
        }
    };

    private Vector<AIDLInterface<ILitePlayerListener>> clientListeners = new Vector<AIDLInterface<ILitePlayerListener>>();

    private enum PLAYER_STATUS {
        PLAYER_IDLE, PLAYER_PREPARING, PLAYER_PREPARED,
    }

    private PLAYER_STATUS mPlayerStatus = PLAYER_STATUS.PLAYER_IDLE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.d(tag, "onCreate  ");
        setResolutionDiv();
        SkinManager.setAppContext(getApplicationContext());
        XServiceManager.init(this);
        new Thread() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    Log.d(tag, "setPlayerKernel  ");
                    XServiceManager.getLitePlayerKernelProxyService().setPlayerKernel(myPlayer, info);
                    XServiceManager.getLitePlayerKernelProxyService().setPlayerKernelMonitor(myMonitor);
                    Log.d(tag, "setPlayerKernel success ");
                } catch (RemoteException e) {
                    Log.d(tag, "setPlayerKernel error!!!!!!!!! ");
                    e.printStackTrace();
                }
            }

        }.start();
        contentView = new PlayerView(this);
        setContentView(contentView);

        bvv = contentView.getBVV();
        initBVideoView(bvv);
        contentView.setPlayerExecutor(PlayerActivity.this);
        resolveIntent();
        Log.i("pa", " playerActivity - onCreate finished");
    }

    private void resolveIntent() {
        Intent intent = this.getIntent();
        Log.d(tag, "resolveIntent intent:" + intent);
        if (intent != null) {
            Uri uri = intent.getData();
            String type = intent.getType();
            if (uri != null) {
                String url = uri.toString();
                String name = url.substring(url.lastIndexOf("/") + 1);
                Log.d(tag, "resolveIntent  uri:" + uri.toString() + " type:" + type + " name:"
                        + name);
                try {
                    VideoSourceInfo info = new VideoSourceInfo();
                    info.url = url;
                    myPlayer.start(info, name);
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    Log.d(tag, "play error!!!");
                    e.printStackTrace();
                }
            }
        }
    }

    public void setResolutionDiv() {
        int width = SkySystemUtil.getDisplayWidth(this);
        float div = 1;
        switch (width) {
            case 3840:
                div = 2.0f;
                break;
            case 1920:
                div = 1.0f;
                break;
            case 1366:
                div = 0.7115f;
                break;
            case 1280:
                div = 0.6667f;
                break;
            default:
                div = 1;
                break;
        }
        SkyConstants.initResolution(this, div, div / SkySystemUtil.getDisplayDensity(this));
        LogUtils.d("the div is " + SkyConstants.mDiv);
    }

    private void initBVideoView(BVideoView bvv) {
        Log.i("pa", "initBVideoView");
        bvv.setOnPreparedListener(this);
        bvv.setOnCompletionListener(this);
        bvv.setOnErrorListener(this);
        bvv.setOnInfoListener(this);
        bvv.setOnPlayingBufferCacheListener(this);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        //不能设置为空
        try {
            XServiceManager.getLitePlayerKernelProxyService().clearPlayerKernel();
            XServiceManager.getLitePlayerKernelProxyService().clearPlayerKernelMonitor();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
//        XServiceManager.uninit(this);  //如果主线程调uninit会报错
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        finish();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stubtry
        if (contentView.isPaused()) {
            synchronized (clientListeners) {
                for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onPlayResumed();
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        if (bvv.isPlaying()) {
            synchronized (clientListeners) {
                for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onPlayPaused();
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }
        super.onPause();
    }

    // ServiceConnection conn = new ServiceConnection()
    // {
    // @Override
    // public void onServiceDisconnected(ComponentName name)
    // {
    // // TODO Auto-generated method stub
    // Log.i(tag, "===== playerActivity - service disconnected!!");
    // try
    // {
    // // playerService.stop();
    // playerService.setServiceToActivityListener(null);
    // playerService.setPlayerBound(false);
    // } catch (RemoteException e)
    // {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // playerService = null;
    // }
    //
    // @Override
    // public void onServiceConnected(ComponentName name, IBinder service)
    // {
    // // TODO Auto-generated method stub
    // Log.i(tag, "===== playerActivity - service connected");
    // playerService = (PlayerServiceStub) IPlayerService.Stub.asInterface(service);
    // contentView.setPlayerExecutor(PlayerActivity.this);
    // try
    // {
    // playerService.setPlayerBound(true);
    // playerService.setServiceToActivityListener(s2aListener);
    // } catch (RemoteException e)
    // {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }
    // };

//    private IServiceToActivityListener s2aListener = new IServiceToActivityListener.Stub()
//    {
//        @Override
//        public void getReady(final String videoName) throws RemoteException
//        {
//            // TODO Auto-generated method stub
//            Log.i(tag, "getReady -0000-- videoName:" + videoName);
//            runOnUiThread(new Runnable()
//            {
//                @Override
//                public void run()
//                {
//                    // TODO Auto-generated method stub
//                    Log.i(tag, "getReady --- videoName:" + videoName);
//                    contentView.showBVV(false);
//                    if (videoName != null && videoName.trim().length() > 0)
//                    {
//                        String loadingStr = String.format("%s\n%s", videoName.trim(),
//                                PlayerActivity.this.getString(R.string.player_loading));
//                        contentView.resetInfo();
//                        contentView.startLoading(loadingStr);
//                    } else
//                    {
//                        contentView.startLoading();
//                    }
//
//                }
//            });
//        }
//
//        @Override
//        public boolean start(Uri uri, String videoName1) throws RemoteException
//        {
//            // TODO Auto-generated method stub
//            Log.i(tag, "Service - start, uri = " + uri + ", videoName = " + videoName1
//                    + " mPlayerStatus:" + mPlayerStatus.toString());
//            if (bvv != null)
//            {
//                if (mPlayerStatus != PLAYER_STATUS.PLAYER_IDLE)
//                {
//                    Log.i(tag, "Start  need stop player!!!");
//                    contentView.setNeedExitActivity(false);
//                    bvv.stopPlayback();
//                }
//
//                while (mPlayerStatus != PLAYER_STATUS.PLAYER_IDLE)
//                {
//                    try
//                    {
//                        Log.i(tag, "Start  wait for player  ");
//                        Thread.currentThread().sleep(10);
//                    } catch (InterruptedException e)
//                    {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//                }
//
//                try
//                {
//                    Log.i(tag, "Start  wait for player again ");
//                    Thread.currentThread().sleep(1000);
//                } catch (InterruptedException e)
//                {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//
//                bvv.setVideoPath(uri.toString());
//                videoName = videoName1;
//                bvv.start();
//                contentView.setNeedExitActivity(true);
//
//                Log.i(tag, "Start  start play");
//                mPlayerStatus = PLAYER_STATUS.PLAYER_PREPARING;
//                scheduleProgressUpdate();
//            }
//            return true;
//        }
//
//        @Override
//        public boolean pause() throws RemoteException
//        {
//            // TODO Auto-generated method stub
//            Log.i(tag, "Service - pause");
//            if (bvv == null)
//            {
//                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
//                return false;
//            }
//            unScheduleProgressUpdate();
//            bvv.pause();
//            contentView.onPlayPaused();
//            return true;
//        }
//
//        @Override
//        public boolean resume() throws RemoteException
//        {
//            // TODO Auto-generated method stub
//            Log.i(tag, "Service - resume");
//            if (bvv == null)
//            {
//                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
//                return false;
//            }
//            scheduleProgressUpdate();
//            bvv.resume();
//            contentView.onPlayResumed();
//            return true;
//        }
//
//        @Override
//        public boolean stop() throws RemoteException
//        {
//            // TODO Auto-generated method stub
//            Log.i(tag, "Service - stop");
//            if (bvv == null)
//            {
//                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
//                return false;
//            }
//            videoName = null;
//            unScheduleProgressUpdate();
//            bvv.stopPlayback();
//            playerService.onPlayStopped();
//            contentView.onPlayStopped();
//            return true;
//        }
//
//        @Override
//        public boolean seekTo(int milliSeconds) throws RemoteException
//        {
//            // TODO Auto-generated method stub
//            Log.i(tag, "Service - seekto  milliSeconds:" + milliSeconds);
//            if (bvv == null)
//            {
//                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
//                return false;
//            }
//            contentView.showSeekPanel(milliSeconds / 1000);
//            bvv.seekTo(milliSeconds / 1000);
//            return true;
//        }
//
//        @Override
//        public boolean isPlaying() throws RemoteException
//        {
//            // TODO Auto-generated method stub
//            Log.i(tag, "Service - isPlaying");
//            if (bvv != null)
//            {
//                return bvv.isPlaying();
//            }
//            Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
//            return false;
//        }
//
//        @Override
//        public int getCurrentTimeInMillis() throws RemoteException
//        {
//            // TODO Auto-generated method stub
//            Log.i(tag, "Service - getCurrentTimeInMillis");
//            if (bvv == null)
//            {
//                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
//                return -1;
//            }
//            return bvv.getCurrentPosition();
//        }
//
//        @Override
//        public int getDuration() throws RemoteException
//        {
//            // TODO Auto-generated method stub
//            Log.i(tag, "Service - getDuration");
//            if (bvv == null)
//            {
//                Log.i(tag, "       mediaplayer = null!!!!!!!!!!!!!!!!");
//                return -1;
//            }
//            return bvv.getDuration();
//        }
//
//        @Override
//        public void errorOccurred(String errorType, String videoName1) throws RemoteException
//        {
//            // TODO Auto-generated method stub
//            videoName = videoName1;
//            contentView.errorOccurred(errorType, videoName1);
//        }
//    };

    private void scheduleProgressUpdate() {
        if (mTimerTask != null) {
            mTimerTask.cancel();
        }
        if (mTimer != null) {
            mTimer.cancel();
        }
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                if (bvv == null)
                    return;
                if (bvv.isPlaying()) {
                    if (videoName != null && bvv.getCurrentPosition() == 1) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                contentView.showBVV(true);
                            }
                        });
                        contentView.onPlayStarted(videoName, bvv.getDuration());
                        synchronized (clientListeners) {
                            for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                                AsyncTask.runOnThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            i.i.onPlayStarted(videoName, bvv.getDuration() * 1000);
                                        } catch (RemoteException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            }
                        }
                        // Log.i(tag,
                        // "scheduleProgressUpdate  vname:"+videoName+" duration:"+bvv.getDuration());
                    }
                    Log.d(tag,
                            "scheduleProgressUpdate   CurrentPosition:" + bvv.getCurrentPosition()
                                    + "  " + bvv.getDuration());
                    contentView.onProgressChanged(bvv.getCurrentPosition());
                    synchronized (clientListeners) {
                        for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                            AsyncTask.runOnThread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        i.i.onProgressChanged(bvv.getCurrentPosition() * 1000);
                                    } catch (RemoteException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    }
                }
            }
        };

        mTimer.schedule(mTimerTask, 0, UpdateProgressPeriod);
    }

    private void unScheduleProgressUpdate() {
        Log.d("play", "unScheduleProgressUpdate");
        if (mTimerTask != null)
            mTimerTask.cancel();
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (contentView.processKeyDown(keyCode))
            return true;
        return super.onKeyDown(keyCode, event);
    }

    // PlayerView PlayerExecutor

    @Override
    public void seekTo(int milliSeconds) {
        // TODO Auto-generated method stub
        bvv.seekTo(milliSeconds); // 遥控器的seek是按照秒来进行的，所以不能用s2aListener的方法
    }

    @Override
    public boolean isPlaying() {
        // TODO Auto-generated method stub
        boolean isPlaying = bvv.isPlaying();
        Log.d(tag, "isPlaying :" + isPlaying);
        return isPlaying;
    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

        try {
            myPlayer.pause();
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        synchronized (clientListeners) {
            for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                AsyncTask.runOnThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            i.i.onPlayPaused();
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
//        try
//        {
//            s2aListener.pause();
//            playerService.onPlayPaused();
//        } catch (RemoteException e)
//        {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub
        try {
            myPlayer.resume();
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        synchronized (clientListeners) {
            for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                AsyncTask.runOnThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            i.i.onPlayResumed();
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
//        try
//        {
//            s2aListener.resume();
//            playerService.onPlayResumed();
//        } catch (RemoteException e)
//        {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
    }

    @Override
    public boolean hasNext() {
        // TODO Auto-generated method stub
        if (nextUri != null)
            return true;
        return false;
    }

    // bvv回调的接口

    @Override
    public void onSeekComplete() // 百度播放器有问题，不会有回调
    {
        // TODO Auto-generated method stub
        Log.i("bvv", "onSeekComplete!!!");
        contentView.onSeekComplete();
        // try
        // {
        // playerService.onSeekComplete();
        // } catch (RemoteException e)
        // {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
    }

    @Override
    public void onPrepared() {
        // TODO Auto-generated method stub
        Log.i("bvv", "onPrepared duration:" + bvv.getDuration());
        if (videoName != null && contentView.videoType == 2) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    contentView.showBVV(true);
                }
            });
            contentView.onPlayStarted(videoName, bvv.getDuration());
            synchronized (clientListeners) {
                for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onPlayStarted(videoName, bvv.getDuration());
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
//            try
//            {
//                playerService.onPlayStarted(videoName, bvv.getDuration());
//            } catch (RemoteException e)
//            {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
        }
        mPlayerStatus = PLAYER_STATUS.PLAYER_PREPARED;
    }

    @Override
    public boolean onInfo(final int what, final int extra) // what为3不知道是个啥消息
    {
        // TODO Auto-generated method stub
        Log.i("bvv", "onInfo ");
        synchronized (clientListeners) {
            for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                AsyncTask.runOnThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            i.i.onError(what, extra);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
//        try
//        {
//            playerService.onInfo(what, extra);
//        } catch (RemoteException e)
//        {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
        contentView.onInfo(what, extra);
        return true;
    }

    @Override
    public boolean onError(final int what, final int extra) {
        // TODO Auto-generated method stubif(mp != null)
        synchronized (clientListeners) {
            for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                AsyncTask.runOnThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            i.i.onError(what, extra);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
//        try
//        {
//            playerService.onError(what, extra);
//        } catch (RemoteException e)
//        {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
        contentView.onError(what, extra);
        mPlayerStatus = PLAYER_STATUS.PLAYER_IDLE;
        return true;
    }

    @Override
    public void onCompletion() {
        // TODO Auto-generated method stub
        Log.i("bvv", "onCompletion ");
        synchronized (clientListeners) {
            for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                AsyncTask.runOnThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            i.i.onCompletion();
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
//        try
//        {
//            playerService.onCompletion();
//        } catch (RemoteException e)
//        {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
        contentView.onCompletion();
        mPlayerStatus = PLAYER_STATUS.PLAYER_IDLE;
    }

    @Override
    public void onPlayingBufferCache(final int percent) // 不会有任何回调的
    {
        Log.i("bvv", "onPlayingBufferCache percent:" + percent);

        synchronized (clientListeners) {
            for (final AIDLInterface<ILitePlayerListener> i : clientListeners) {
                AsyncTask.runOnThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            i.i.onBufferingUpdate(percent);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }

        contentView.onBufferingUpdate(percent);
    }

}
